args S Controls lab keep ipw CreativityMeasure a1 a2 a3 a4 a5 a6 a7 a8


preserve

* setup
replace Creativity=creativityt_`CreativityMeasure'_7


local lwage  lwage_I23  lwage_I33  lwage_I42  lwage_I46  lwage_I50  lwage_I55
local AE     AE_I23     AE_I33     AE_I42     AE_I46     AE_I50     AE_I55
local lpay   lpay_I23   lpay_I33   lpay_I42   lpay_I46   lpay_I50   lpay_I55  
local lhours  lhours_I23 lhours_I33 lhours_I42 lhours_I46 lhours_I50 lhours_I55  


local O  lwage lpay lhours AE
local O1 lwage lpay lhours
local O2 AE


* calculate ipw weights (including self employed)
foreach o of local O {

	foreach y of local `o' {
		if `ipw'==1{
			ge io=`y'!=.
			qui logit io $HDC_0
			drop io
			predict p_`y', pr
		}

		if `ipw'==0{
			ge  p_`y'=1
		}

	}	
}

* calculate ipw weights (excluding self employed)
foreach o of local O1 {
	foreach y of local `o' {

		if `ipw'==1{
			local a=substr("`y'",-3,.) /* find instrument and age*/
			ge io=`y'!=. if SE_`a'==0
			qui logit io $HDC_0
			drop io
			predict pse_`y', pr
			replace pse_`y'=1 if MissingMA_0==1 & pse_`y'==. & p_`y'!=. /* fix for 100% successful prediction */
		}

		if `ipw'==0{
			ge  pse_`y'=1
		}

	}	
}

* reshape to long dataset
reshape long lwage_I lpay_I lhours_I AE_I SE_I p_lwage_I p_lpay_I p_lhours_I p_AE_I pse_lwage_I pse_lpay_I pse_lhours_I cCSC_1_I cCSC_2_I cCSC_3_I cCSC_4_I cCSC_5_I cCSC_6_I cCSC_7_I, i(NCDSID) j(age)
su p_*



* estimation including self employed
foreach o of local O1 {

	qui rifhdreg `o' `Controls' [pweight=1/p_`o'], rif(mean) cluster(NCDSID)

	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]

	qui estadd scalar Ni=e(N_clust) 
	qui estadd local mdv "-"
	est sto es_`o'
}




foreach o of local O2 {

	qui rifhdreg `o' `Controls' [pweight=1/p_`o'], rif(mean) cluster(NCDSID)

	qui estadd scalar Ni=e(N_clust) 
	estadd scalar mdv=e(rifmean) 

	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]

	est sto es_`o'
}



* estimation excluding self employed
foreach o of local O1 {

	rifhdreg `o' `Controls' [pweight=1/pse_`o'] if SE==0, rif(mean) cluster(NCDSID)

	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]

	qui estadd scalar Ni=e(N_clust) 
	est sto nose_es_`o'

}



* Stats
local stP "stats( N mdv, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  "Mean dep. var."))" 




* count number of variables
local count0: word count `O'
local count=`count0'+1

if `S' < 7{
	
	

	* without p-values
	******************************************************************************************


	local text0 "prehead( \vspace{.2cm}  \\ \multicolumn{4}{l}{\textbf{Panel A: All individuals}} \\ \multicolumn{4}{l}{\phantom{.}} \\ \vspace{-.6cm}  \\  & \multicolumn{3}{c}{Individuals in work} &    \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
	local text  `text0' prefoot( \cline{2-`count'}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-`count'} )  postfoot(\vspace{-.3cm} \\ \hline ) 

	* Stats
	local stP "stats( N mdv, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  "Mean dep. var."))" 

	* all individuals
	esttab es* using results/Tables/R_OLS_`lab'.tex, ///
		replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
		cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'


	local text0 "prehead( \vspace{.2cm}  \\ \multicolumn{4}{l}{\textbf{Panel B: Excluding self-employed}} \\ \multicolumn{4}{l}{\phantom{.}} \\ \vspace{-.6cm}  \\  & \multicolumn{3}{c}{Individuals in work} &  \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
	local text  `text0' prefoot( \cline{2-`count0'}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-`count0'}) 

	local stP "stats( N, fmt( %11.0gc %8.2f)  labels("Individual-year obs." ))" 

	* without self employed
	esttab nose_es* using results/Tables/R_OLS_`lab'.tex, ///
		append `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
		cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'


	* with p-values
	******************************************************************************************

	local text0 "prehead( \vspace{.2cm}  \\ \multicolumn{4}{l}{\textbf{Panel A: All individuals}} \\ \multicolumn{4}{l}{\phantom{.}} \\ \vspace{-.6cm}  \\  & \multicolumn{3}{c}{Individuals in work} &    \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
	local text  `text0' prefoot( \cline{2-`count'}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-`count'} )  postfoot(\vspace{-.3cm} \\ \hline ) 

	* Stats
	local stP "stats( N mdv, fmt( %11.0gc %8.2f)  labels("Individual-year obs."  "Mean dep. var."))" 

	* all individuals
	esttab es* using results/Tables/R_OLS_`lab'p.tex, ///
		replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
		cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'


	local text0 "prehead( \vspace{.2cm}  \\ \multicolumn{4}{l}{\textbf{Panel B: Excluding self-employed}} \\ \multicolumn{4}{l}{\phantom{.}} \\ \vspace{-.6cm}  \\  & \multicolumn{3}{c}{Individuals in work} &  \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
	local text  `text0' prefoot( \cline{2-`count0'}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-`count0'}) 

	local stP "stats( N, fmt( %11.0gc %8.2f)  labels("Individual-year obs." ))" 

	* without self employed
	esttab nose_es* using results/Tables/R_OLS_`lab'p.tex, ///
		append `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
		cells(b(star fmt(3)) se(par) p(par([ ]) fmt(%9.5f)) ".") nogaps   alignment(c) ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'

}


if `S' == 7{

	* without p-values, without in work
	******************************************************************************************

	estimates drop es_AE

	local text0 "prehead( \vspace{.2cm}  \\ \multicolumn{4}{l}{\textbf{Panel A: All individuals}} \\ \multicolumn{4}{l}{\phantom{.}} \\ \vspace{-.6cm}  \\  & \multicolumn{3}{c}{Individuals in work}   \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
	local text  `text0' prefoot( \cline{2-4}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-4} )  postfoot(\vspace{-.3cm} \\ \hline ) 

	* Stats
	local stP "stats( N, fmt( %11.0gc %8.2f)  labels("Individual-year obs." ))" 

	* all individuals
	esttab es* using results/Tables/R_OLS_`lab'_EmpOnly.tex, ///
		replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
		cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'


	local text0 "prehead( \vspace{.2cm}  \\ \multicolumn{4}{l}{\textbf{Panel B: Excluding self-employed}} \\ \multicolumn{4}{l}{\phantom{.}} \\ \vspace{-.6cm}  \\  & \multicolumn{3}{c}{Individuals in work}  \\ \vspace{-.25cm} \\ \cline{2-4} \\ \vspace{-.65cm}\\ ) "
	local text  `text0' prefoot( \cline{2-4}  \vspace{-.3cm}\\ )  posthead(\vspace{-.3cm}\\ \cline{2-4}) 

	local stP "stats( N, fmt( %11.0gc %8.2f)  labels("Individual-year obs." ))" 

	* without self employed
	esttab nose_es* using results/Tables/R_OLS_`lab'_EmpOnly.tex, ///
		append `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability") ///
		cells(b(star fmt(3)) se(par) ".") nogaps   alignment(c) ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stP'
		
	

}





est drop _all
restore

